clc 
close all
clear all
%%PRKS
prks_pdr95_rd1 = GetStableRegionDelay ('PRKS95_delay_rd1.txt');
prks_pdr95_rd2 = GetStableRegionDelay ('PRKS95_delay_rd2.txt');
prks_pdr95_rd3 = GetStableRegionDelay ('PRKS95_delay_rd3.txt');
prks_pdr95_rd4 = GetStableRegionDelay ('PRKS95_delay_rd4.txt');
prks_pdr95_rd5 = GetStableRegionDelay ('PRKS95_delay_rd5.txt');
prks_pdr95 = [prks_pdr95_rd1; prks_pdr95_rd2;prks_pdr95_rd3;prks_pdr95_rd4;prks_pdr95_rd5];

prks_pdr90_rd1 = GetStableRegionDelay ('PRKS90_delay_rd1.txt');
prks_pdr90_rd2 = GetStableRegionDelay ('PRKS90_delay_rd2.txt');
prks_pdr90_rd3 = GetStableRegionDelay ('PRKS90_delay_rd3.txt');
prks_pdr90_rd4 = GetStableRegionDelay ('PRKS90_delay_rd4.txt');
prks_pdr90_rd5 = GetStableRegionDelay ('PRKS90_delay_rd5.txt');
prks_pdr90 = [prks_pdr90_rd1; prks_pdr90_rd2;prks_pdr90_rd3;prks_pdr90_rd4;prks_pdr90_rd5];

prks_pdr80_rd1 = GetStableRegionDelay ('PRKS80_delay_rd1.txt');
prks_pdr80_rd2 = GetStableRegionDelay ('PRKS80_delay_rd2.txt');
prks_pdr80_rd3 = GetStableRegionDelay ('PRKS80_delay_rd3.txt');
prks_pdr80_rd4 = GetStableRegionDelay ('PRKS80_delay_rd4.txt');
prks_pdr80_rd5 = GetStableRegionDelay ('PRKS80_delay_rd5.txt');
prks_pdr80 = [prks_pdr80_rd1; prks_pdr80_rd2;prks_pdr80_rd3;prks_pdr80_rd4;prks_pdr80_rd5];

prks_pdr70_rd1 = GetStableRegionDelay ('PRKS70_delay_rd1.txt');
prks_pdr70_rd2 = GetStableRegionDelay ('PRKS70_delay_rd2.txt');
prks_pdr70_rd3 = GetStableRegionDelay ('PRKS70_delay_rd3.txt');
prks_pdr70_rd4 = GetStableRegionDelay ('PRKS70_delay_rd4.txt');
prks_pdr70_rd5 = GetStableRegionDelay ('PRKS70_delay_rd5.txt');
prks_pdr70 = [prks_pdr70_rd1; prks_pdr70_rd2;prks_pdr70_rd3;prks_pdr70_rd4;prks_pdr70_rd5];


%%PRKS-L
prks_l_pdr95_rd1 = GetStableRegionDelay ('PRKS_L95_delay_rd1.txt');
prks_l_pdr95_rd2 = GetStableRegionDelay ('PRKS_L95_delay_rd2.txt');
prks_l_pdr95_rd3 = GetStableRegionDelay ('PRKS_L95_delay_rd3.txt');
prks_l_pdr95_rd4 = GetStableRegionDelay ('PRKS_L95_delay_rd4.txt');
prks_l_pdr95_rd5 = GetStableRegionDelay ('PRKS_L95_delay_rd5.txt');
prks_l_pdr95 = [prks_l_pdr95_rd1; prks_l_pdr95_rd2;prks_l_pdr95_rd3;prks_l_pdr95_rd4;prks_l_pdr95_rd5];

prks_l_pdr90_rd1 = GetStableRegionDelay ('PRKS_L90_delay_rd1.txt');
prks_l_pdr90_rd2 = GetStableRegionDelay ('PRKS_L90_delay_rd2.txt');
prks_l_pdr90_rd3 = GetStableRegionDelay ('PRKS_L90_delay_rd3.txt');
prks_l_pdr90_rd4 = GetStableRegionDelay ('PRKS_L90_delay_rd4.txt');
prks_l_pdr90_rd5 = GetStableRegionDelay ('PRKS_L90_delay_rd5.txt');
prks_l_pdr90 = [prks_l_pdr90_rd1; prks_l_pdr90_rd2;prks_l_pdr90_rd3;prks_l_pdr90_rd4;prks_l_pdr90_rd5];

prks_l_pdr80_rd1 = GetStableRegionDelay ('PRKS_L80_delay_rd1.txt');
prks_l_pdr80_rd2 = GetStableRegionDelay ('PRKS_L80_delay_rd2.txt');
prks_l_pdr80_rd3 = GetStableRegionDelay ('PRKS_L80_delay_rd3.txt');
prks_l_pdr80_rd4 = GetStableRegionDelay ('PRKS_L80_delay_rd4.txt');
prks_l_pdr80_rd5 = GetStableRegionDelay ('PRKS_L80_delay_rd5.txt');
prks_l_pdr80 = [prks_l_pdr80_rd1; prks_l_pdr80_rd2;prks_l_pdr80_rd3;prks_l_pdr80_rd4;prks_l_pdr80_rd5];

prks_l_pdr70_rd1 = GetStableRegionDelay ('PRKS_L70_delay_rd1.txt');
prks_l_pdr70_rd2 = GetStableRegionDelay ('PRKS_L70_delay_rd2.txt');
prks_l_pdr70_rd3 = GetStableRegionDelay ('PRKS_L70_delay_rd3.txt');
prks_l_pdr70_rd4 = GetStableRegionDelay ('PRKS_L70_delay_rd4.txt');
prks_l_pdr70_rd5 = GetStableRegionDelay ('PRKS_L70_delay_rd5.txt');
prks_l_pdr70 = [prks_l_pdr70_rd1; prks_l_pdr70_rd2;prks_l_pdr70_rd3;prks_l_pdr70_rd4;prks_l_pdr70_rd5];

%%PRKS-R
prks_r_pdr95_rd1 = GetStableRegionDelay ('PRKS_R95_delay_rd1.txt');
prks_r_pdr95_rd2 = GetStableRegionDelay ('PRKS_R95_delay_rd2.txt');
prks_r_pdr95_rd3 = GetStableRegionDelay ('PRKS_R95_delay_rd3.txt');
prks_r_pdr95_rd4 = GetStableRegionDelay ('PRKS_R95_delay_rd4.txt');
prks_r_pdr95_rd5 = GetStableRegionDelay ('PRKS_R95_delay_rd5.txt');
prks_r_pdr95 = [prks_r_pdr95_rd1; prks_r_pdr95_rd2;prks_r_pdr95_rd3;prks_r_pdr95_rd4;prks_r_pdr95_rd5];

prks_r_pdr90_rd1 = GetStableRegionDelay ('PRKS_R90_delay_rd1.txt');
prks_r_pdr90_rd2 = GetStableRegionDelay ('PRKS_R90_delay_rd2.txt');
prks_r_pdr90_rd3 = GetStableRegionDelay ('PRKS_R90_delay_rd3.txt');
prks_r_pdr90_rd4 = GetStableRegionDelay ('PRKS_R90_delay_rd4.txt');
prks_r_pdr90_rd5 = GetStableRegionDelay ('PRKS_R90_delay_rd5.txt');
prks_r_pdr90 = [prks_r_pdr90_rd1; prks_r_pdr90_rd2;prks_r_pdr90_rd3;prks_r_pdr90_rd4;prks_r_pdr90_rd5];

prks_r_pdr80_rd1 = GetStableRegionDelay ('PRKS_R80_delay_rd1.txt');
prks_r_pdr80_rd2 = GetStableRegionDelay ('PRKS_R80_delay_rd2.txt');
prks_r_pdr80_rd3 = GetStableRegionDelay ('PRKS_R80_delay_rd3.txt');
prks_r_pdr80_rd4 = GetStableRegionDelay ('PRKS_R80_delay_rd4.txt');
prks_r_pdr80_rd5 = GetStableRegionDelay ('PRKS_R80_delay_rd5.txt');
prks_r_pdr80 = [prks_r_pdr80_rd1; prks_r_pdr80_rd2;prks_r_pdr80_rd3;prks_r_pdr80_rd4;prks_r_pdr80_rd5];

prks_r_pdr70_rd1 = GetStableRegionDelay ('PRKS_R70_delay_rd1.txt');
prks_r_pdr70_rd2 = GetStableRegionDelay ('PRKS_R70_delay_rd2.txt');
prks_r_pdr70_rd3 = GetStableRegionDelay ('PRKS_R70_delay_rd3.txt');
prks_r_pdr70_rd4 = GetStableRegionDelay ('PRKS_R70_delay_rd4.txt');
prks_r_pdr70_rd5 = GetStableRegionDelay ('PRKS_R70_delay_rd5.txt');
prks_r_pdr70 = [prks_r_pdr70_rd1; prks_r_pdr70_rd2;prks_r_pdr70_rd3;prks_r_pdr70_rd4;prks_r_pdr70_rd5];

%%PRKS-RI
prks_ri_pdr95_rd1 = GetStableRegionDelay ('PRKS_RI95_delay_rd1.txt');
prks_ri_pdr95_rd2 = GetStableRegionDelay ('PRKS_RI95_delay_rd2.txt');
prks_ri_pdr95_rd3 = GetStableRegionDelay ('PRKS_RI95_delay_rd3.txt');
prks_ri_pdr95_rd4 = GetStableRegionDelay ('PRKS_RI95_delay_rd4.txt');
prks_ri_pdr95_rd5 = GetStableRegionDelay ('PRKS_RI95_delay_rd5.txt');
prks_ri_pdr95 = [prks_ri_pdr95_rd1; prks_ri_pdr95_rd2;prks_ri_pdr95_rd3;prks_ri_pdr95_rd4;prks_ri_pdr95_rd5];

prks_ri_pdr90_rd1 = GetStableRegionDelay ('PRKS_RI90_delay_rd1.txt');
prks_ri_pdr90_rd2 = GetStableRegionDelay ('PRKS_RI90_delay_rd2.txt');
prks_ri_pdr90_rd3 = GetStableRegionDelay ('PRKS_RI90_delay_rd3.txt');
prks_ri_pdr90_rd4 = GetStableRegionDelay ('PRKS_RI90_delay_rd4.txt');
prks_ri_pdr90_rd5 = GetStableRegionDelay ('PRKS_RI90_delay_rd5.txt');
prks_ri_pdr90 = [prks_ri_pdr90_rd1; prks_ri_pdr90_rd2;prks_ri_pdr90_rd3;prks_ri_pdr90_rd4;prks_ri_pdr90_rd5];

prks_ri_pdr80_rd1 = GetStableRegionDelay ('PRKS_RI80_delay_rd1.txt');
prks_ri_pdr80_rd2 = GetStableRegionDelay ('PRKS_RI80_delay_rd2.txt');
prks_ri_pdr80_rd3 = GetStableRegionDelay ('PRKS_RI80_delay_rd3.txt');
prks_ri_pdr80_rd4 = GetStableRegionDelay ('PRKS_RI80_delay_rd4.txt');
prks_ri_pdr80_rd5 = GetStableRegionDelay ('PRKS_RI80_delay_rd5.txt');
prks_ri_pdr80 = [prks_ri_pdr80_rd1; prks_ri_pdr80_rd2;prks_ri_pdr80_rd3;prks_ri_pdr80_rd4;prks_ri_pdr80_rd5];

prks_ri_pdr70_rd1 = GetStableRegionDelay ('PRKS_RI70_delay_rd1.txt');
prks_ri_pdr70_rd2 = GetStableRegionDelay ('PRKS_RI70_delay_rd2.txt');
prks_ri_pdr70_rd3 = GetStableRegionDelay ('PRKS_RI70_delay_rd3.txt');
prks_ri_pdr70_rd4 = GetStableRegionDelay ('PRKS_RI70_delay_rd4.txt');
prks_ri_pdr70_rd5 = GetStableRegionDelay ('PRKS_RI70_delay_rd5.txt');
prks_ri_pdr70 = [prks_ri_pdr70_rd1; prks_ri_pdr70_rd2;prks_ri_pdr70_rd3;prks_ri_pdr70_rd4;prks_ri_pdr70_rd5];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ALPHA=0.5;

MEAN = zeros(4,4);
MEAN(1,1) = mean(prks_pdr70(:,3));
MEAN(1,2) = mean(prks_r_pdr70(:,3));
MEAN(1,3) = mean(prks_ri_pdr70(:,3));
MEAN(1,4) = mean(prks_l_pdr70(:,3));
Error_MEAN(1,1) =  norminv(1 - ALPHA / 2, 0, 1) * std(prks_pdr70(:,3)) / sqrt(size(prks_pdr70(:,3), 1));
Error_MEAN(1,2) =  norminv(1 - ALPHA / 2, 0, 1) * std(prks_r_pdr70(:,3)) / sqrt(size(prks_r_pdr70(:,3), 1));
Error_MEAN(1,3) =  norminv(1 - ALPHA / 2, 0, 1) * std(prks_ri_pdr70(:,3)) / sqrt(size(prks_ri_pdr70(:,3), 1));
Error_MEAN(1,4) =  norminv(1 - ALPHA / 2, 0, 1) * std(prks_l_pdr70(:,3)) / sqrt(size(prks_l_pdr70(:,3), 1));
MEAN(2,1) = mean(prks_pdr80(:,3));
MEAN(2,2) = mean(prks_r_pdr80(:,3));
MEAN(2,3) = mean(prks_ri_pdr80(:,3));
MEAN(2,4) = mean(prks_l_pdr80(:,3));
Error_MEAN(2,1) =  norminv(1 - ALPHA / 2, 0, 1) * std(prks_pdr80(:,3)) / sqrt(size(prks_pdr80(:,3), 1));
Error_MEAN(2,2) =  norminv(1 - ALPHA / 2, 0, 1) * std(prks_r_pdr80(:,3)) / sqrt(size(prks_r_pdr80(:,3), 1));
Error_MEAN(2,3) =  norminv(1 - ALPHA / 2, 0, 1) * std(prks_ri_pdr80(:,3)) / sqrt(size(prks_ri_pdr80(:,3), 1));
Error_MEAN(2,4) =  norminv(1 - ALPHA / 2, 0, 1) * std(prks_l_pdr80(:,3)) / sqrt(size(prks_l_pdr80(:,3), 1));
MEAN(3,1) = mean(prks_pdr90(:,3));
MEAN(3,2) = mean(prks_r_pdr90(:,3));
MEAN(3,3) = mean(prks_ri_pdr90(:,3));
MEAN(3,4) = mean(prks_l_pdr90(:,3));
Error_MEAN(3,1) =  norminv(1 - ALPHA / 2, 0, 1) * std(prks_pdr90(:,3)) / sqrt(size(prks_pdr90(:,3), 1));
Error_MEAN(3,2) =  norminv(1 - ALPHA / 2, 0, 1) * std(prks_r_pdr90(:,3)) / sqrt(size(prks_r_pdr90(:,3), 1));
Error_MEAN(3,3) =  norminv(1 - ALPHA / 2, 0, 1) * std(prks_ri_pdr90(:,3)) / sqrt(size(prks_ri_pdr90(:,3), 1));
Error_MEAN(3,4) =  norminv(1 - ALPHA / 2, 0, 1) * std(prks_l_pdr90(:,3)) / sqrt(size(prks_l_pdr90(:,3), 1));
MEAN(4,1) = mean(prks_pdr95(:,3));
MEAN(4,2) = mean(prks_r_pdr95(:,3));
MEAN(4,3) = mean(prks_ri_pdr95(:,3));
MEAN(4,4) = mean(prks_l_pdr95(:,3));
Error_MEAN(4,1) =  norminv(1 - ALPHA / 2, 0, 1) * std(prks_pdr95(:,3)) / sqrt(size(prks_pdr95(:,3), 1));
Error_MEAN(4,2) =  norminv(1 - ALPHA / 2, 0, 1) * std(prks_r_pdr95(:,3)) / sqrt(size(prks_r_pdr95(:,3), 1));
Error_MEAN(4,3) =  norminv(1 - ALPHA / 2, 0, 1) * std(prks_ri_pdr95(:,3)) / sqrt(size(prks_ri_pdr95(:,3), 1));
Error_MEAN(4,4) =  norminv(1 - ALPHA / 2, 0, 1) * std(prks_l_pdr95(:,3)) / sqrt(size(prks_l_pdr95(:,3), 1));

figure;
%handle = barweb(MEAN, Error_MEAN,1, ['70%';'80%';'90%';'95%'], [], ['PDR requirement'], ['Mean Delay'], [1 1 1; 0.7 0.7 0.7; 0.2 0.2 0.2; 0 0 0]);
handle = barweb(MEAN, Error_MEAN,1, ['70%';'80%';'90%';'95%'], [], ['PDR requirement'], ['Mean Delay'], jet);
handle.legend =legend ('PRKS','PRKS-R','PRKS-RI','PRKS-L','orientation','horizontal');
box on
maximize ();
%saveas(gcf, 'mean_delay_slot_all_variants.jpg');
%saveas(gcf, 'mean_delay_slot_all_variants.eps');
%saveas(gcf, 'mean_delay_slot_all_variants.fig');

%{

MEDIAN = zeros(4,4);
MEDIAN(1,1) = median(delay_pro_70(:,3));
MEDIAN(1,2) = median(csma_delay(:,3));
MEDIAN(1,3) = median(rtscts_delay(:,3));
MEDIAN(1,4) = median(delay_rid_70(:,3));
Error_MEDIAN(1,1) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_pro_70(:,3)) / sqrt(size(delay_pro_70(:,3), 1));
Error_MEDIAN(1,2) =  norminv(1 - ALPHA / 2, 0, 1) * std(csma_delay(:,3)) / sqrt(size(csma_delay(:,3), 1));
Error_MEDIAN(1,3) =  norminv(1 - ALPHA / 2, 0, 1) * std(rtscts_delay(:,3)) / sqrt(size(rtscts_delay(:,3), 1));
Error_MEDIAN(1,4) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_rid_70(:,3)) / sqrt(size(delay_rid_70(:,3), 1));
MEDIAN(2,1) = median(delay_pro_80(:,3));
MEDIAN(2,2) = median(csma_delay(:,3));
MEDIAN(2,3) = median(rtscts_delay(:,3));
MEDIAN(2,4) = median(delay_rid_80(:,3));
Error_MEDIAN(2,1) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_pro_80(:,3)) / sqrt(size(delay_pro_80(:,3), 1));
Error_MEDIAN(2,2) =  norminv(1 - ALPHA / 2, 0, 1) * std(csma_delay(:,3)) / sqrt(size(csma_delay(:,3), 1));
Error_MEDIAN(2,3) =  norminv(1 - ALPHA / 2, 0, 1) * std(rtscts_delay(:,3)) / sqrt(size(rtscts_delay(:,3), 1));
Error_MEDIAN(2,4) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_rid_80(:,3)) / sqrt(size(delay_rid_80(:,3), 1));
MEDIAN(3,1) = median(delay_pro_90(:,3));
MEDIAN(3,2) = median(csma_delay(:,3));
MEDIAN(3,3) = median(rtscts_delay(:,3));
MEDIAN(3,4) = median(delay_rid_90(:,3));
Error_MEDIAN(3,1) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_pro_90(:,3)) / sqrt(size(delay_pro_90(:,3), 1));
Error_MEDIAN(3,2) =  norminv(1 - ALPHA / 2, 0, 1) * std(csma_delay(:,3)) / sqrt(size(csma_delay(:,3), 1));
Error_MEDIAN(3,3) =  norminv(1 - ALPHA / 2, 0, 1) * std(rtscts_delay(:,3)) / sqrt(size(rtscts_delay(:,3), 1));
Error_MEDIAN(3,4) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_rid_90(:,3)) / sqrt(size(delay_rid_90(:,3), 1));
MEDIAN(4,1) = median(delay_pro_95(:,3));
MEDIAN(4,2) = median(csma_delay(:,3));
MEDIAN(4,3) = median(rtscts_delay(:,3));
MEDIAN(4,4) = median(delay_rid_95(:,3));
Error_MEDIAN(4,1) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_pro_95(:,3)) / sqrt(size(delay_pro_95(:,3), 1));
Error_MEDIAN(4,2) =  norminv(1 - ALPHA / 2, 0, 1) * std(csma_delay(:,3)) / sqrt(size(csma_delay(:,3), 1));
Error_MEDIAN(4,3) =  norminv(1 - ALPHA / 2, 0, 1) * std(rtscts_delay(:,3)) / sqrt(size(rtscts_delay(:,3), 1));
Error_MEDIAN(4,4) =  norminv(1 - ALPHA / 2, 0, 1) * std(delay_rid_95(:,3)) / sqrt(size(delay_rid_95(:,3), 1));

figure;
handle = barweb(MEDIAN, Error_MEDIAN,1, ['70%';'80%';'90%';'95%'], [], ['PDR requirement'], ['Median Delay'], [1 1 1; 0.7 0.7 0.7; 0.2 0.2 0.2; 0 0 0]);
handle.legend =legend ('PRKS','B-MAC','S-MAC','RIDB','orientation','horizontal');
set(gca,'fontsize',60,'YScale','log');
set(gca,'fontsize',60);
box on
maximize ();
saveas(gcf, 'median_delay_slot_across_different_protocols.jpg');
saveas(gcf, 'median_delay_slot_across_different_protocols.eps');
saveas(gcf, 'median_delay_slot_across_different_protocols.fig');
%}
